package com.bardpub.controls 
{
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName BComponent.as
	 * @author LittleBard
	 * @version 1.0
	 * @date 2010/4/28
	 * @descrition BardPub Component 基类
	 * @others
	 * @history
	 */
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.getDefinitionByName;
	
	public class BComponent extends Sprite
	{
		public static const DRAW:String = "draw";
		
		private var __enabled:Boolean;
		private var __width:Number;
		private var __height:Number;
		
		public function BComponent(parent:DisplayObjectContainer = null, x:Number = 0, y:Number = 0) 
		{
			if (super.scaleX != 1 || super.scaleY != 1)
			{
				__width = super.width;
				__height = super.height;
				super.scaleX = super.scaleY = 1;
			}
			else 
			{
				__width = super.width;
				__height = super.height;
			}
			
			clean();
			__enabled = true;
			
			if (this.parent == null)
			{
				move(x, y);	
			}
			
			if (parent != null) 
			{
				parent.addChild(this);
			}
			
			initialize();
			
			if (stage)
			{
				onAddedToStage(null);
			}
			else 
			{
				addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			}
		}
		
		protected function onAddedToStage(e:Event):void 
		{
			
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
		}
		
		private function clean():void {
			
			for (var i:int = this.numChildren - 1; i > -1; i-- )
			{
				removeChildAt(i);
			}
		}
		
		
		/**
		 * 初始化组件
		 */
		protected function initialize():void 
		{
			addChildren();
			invalidate();
		}
		
		/**
		 * 虚方法，添加子显示对象
		 */
		protected  function addChildren():void 
		{
			
		}
		
		/**
		 * 确定组件在下一帧渲染
		 */
		protected function invalidate():void 
		{
			addEventListener(Event.ENTER_FRAME, onInvalidate);
		}
		
		protected function onInvalidate(event:Event):void 
		{
			removeEventListener(Event.ENTER_FRAME, onInvalidate);
			draw();
		}
		
		/**
		 * 根据类名获取类
		 * @param	className 要获取的类名
		 */
		protected function getClassByName(className:String):Class 
		{
			var ref:Class;
			try 
			{
				ref = getDefinitionByName(className) as Class;
			}
			catch (e:*)
			{
				throw new Error("类 " + className + "不存在");
			}
			return ref;
		}
		
/*=================================public methods====================================*/
		/**
		 * 虚方法，绘制组件
		 */
		public function draw():void 
		{
			dispatchEvent(new Event(DRAW));
		}
		
		/**
		 * 移动组件到指定的位置
		 * @param	x  要移动的x坐标
		 * @param	y  要移动的y坐标
		 */
		public function move(x:Number, y:Number):void 
		{
			this.x = Math.round(x);
			this.y = Math.round(y);
		}
		
		/**
		 * 设置组件大小
		 * @param	width	组件宽度
		 * @param	height	组件高度
		 */
		public function setSize(width:Number, height:Number):void 
		{
			__width = width;
			__height = height;
			invalidate();
		}
		
		
/*=================================getter/setter====================================*/		

		/**
		 * 获取/设置组件宽度
		 */
		override public function get width():Number 
		{
			return __width;
		}
		override public function set width(value:Number):void 
		{
			__width = value;
			invalidate();
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		/**
		 * 获取/设置组件高度
		 */
		override public function get height():Number { return __height }
		override public function set height(value:Number):void 
		{
			__height = value;
			invalidate();
			dispatchEvent(new Event(Event.RESIZE));
		}
		
		
		/**
		 * 设置组件x位置
		 */	
		override public function set x(value:Number):void 
		{
			super.x = Math.round(value);
		}
		
		/**
		 * 设置组件y位置
		 */
		override public function set y(value:Number):void 
		{
			super.y = Math.round(value);
		}
		
		/**
		 * 获取/设置组件活动状态
		 */
		public function get enabled():Boolean 
		{
			return __enabled;
		}
		public function set enabled(value:Boolean):void 
		{
			__enabled = value;
			mouseEnabled = mouseChildren = value;
			alpha = value ? 1 : .5;
		}
	}

}